<?php
/**
 * Galdic project
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Galdic
 * @package    Galdic
 * @copyright  Copyright (c) 2009 vifito (http://vifito.eu)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * Admin_AbbreviationsController
 * 
 * @package    Galdic
 * @copyright  Copyright (c) 2009 vifito (http://vifito.eu)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: AbbreviationsController.php 1 2009-09-12 10:53:47Z vifito $
 */
class Admin_AbbreviationsController extends Zend_Controller_Action
{

    /**
     * Check session
     */
    public function preDispatch()
    {
        $session = Zend_Registry::get('session');
        $actionName = $this->getRequest()->getActionName();
        
        if(!isset($session->username) && ($actionName!='login')) {
            $this->_redirect('/admin/user/login/');
        } elseif( isset($session->username) ) {
            $this->view->username = $session->username;
        }
    }

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->title = "Abreviaturas";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $abbrs = new Admin_Model_DbTable_Abbreviations();
        $rows = $abbrs->fetchAll(null, 'name ASC');
        
        // Pagination
        $paginator = Zend_Paginator::factory($rows);        
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        
        $this->view->assign('abbreviations', $paginator);
    }

    public function addAction()
    {
        $this->view->title = "Nova abreviatura";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $form = new Admin_Form_Abbreviation();
        /*$form->submit->setLabel('Crear');*/
        $this->view->form = $form;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)) {
                $abbr = new Admin_Model_DbTable_Abbreviations();
                
                $data = $form->bindPostData($formData);
                
                $abbr->insert($data);                
                
                $this->_redirect('/admin/abbreviations/'); // Listado de abreviaturas
            } else {
                $form->populate($formData);
            }            
        }
        
        //$this->_helper->viewRenderer->setResponseSegment('abreviaturas');
    }

    public function editAction()
    {
        $this->view->title = "Modificar datos";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $form = new Admin_Form_Abbreviation();
        /*$form->submit->setLabel('Modificar');*/
        $this->view->form = $form;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)) {
                $abbr = new Admin_Model_DbTable_Abbreviations();
                
                $data = $form->bindPostData($formData);                
                unset($data['id']);
                
                $abbr->update($data, '`id`='.(int)$formData['id']);                
                
                $this->_redirect('/admin/abbreviations/'); 
            } else {
                $form->populate($formData);
            }            
        } else {
            $id = $this->_getParam('id', 0);
            if($id > 0) {
                $abbr = new Admin_Model_DbTable_Abbreviations();                
                $form->populate( $abbr->fetchRow('`id`='.(int)$id)->toArray() );
            } else {
                $this->_redirect('/admin/abbreviations/add/');
            }            
        }
    }

    public function deleteAction()
    {
        $this->view->title = "Eliminar abreviatura";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        if($this->getRequest()->isPost()) {
            $action = $this->getRequest()->getPost('action');                        
            if($action == 'Si') {
                $id = $this->getRequest()->getPost('id');
                
                $abbr = new Admin_Model_DbTable_Abbreviations();
                $abbr->delete('`id`='.(int)$id);                                
            }
                
            $this->_redirect('/admin/abbreviations/'); 
        } else {
            $id = $this->_getParam('id', 0);
            $abbr = new Admin_Model_DbTable_Abbreviations();
            $this->view->abbreviation = $abbr->fetchRow('`id`='.(int)$id);
        }
    }


}









